package com.pwt.service;

import com.pwt.common.CustomException;
import com.pwt.common.DataNotFoundException;
import com.pwt.common.ResultCode;
import com.pwt.entity.*;
import com.pwt.model.*;
import com.pwt.repository.*;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * ProductLine service层
 *
 * @author auto generated
 * @date 2021-09-03 11:13:59
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WhatLineService {

    @Autowired
    WhatLineRepository whatLineRepository;

    @Autowired
    WhatLineMachineRepository whatLineMachineRepository;

    @Autowired
    MASCHINERepository maschineRepository;

    @Autowired
    HierarchyRepository hierarchyRepository;

    @Autowired
    HierarchyService hierarchyService;

    @Autowired
    WhatLineHierarchyRepository whatLineHierarchyRepository;

    @Autowired
    AbilityLineService abilityLineService;

    @Autowired
    UserRepository userRepository;

    @Autowired
    PRUEFERRepository prueferRepository;

    @Autowired
    JPAQueryFactory jpaQueryFactory;

    public List<WhatLine> list(String username) {
        QWhatLine qWhatLine = QWhatLine.whatLine;
        List<WhatLine> list = jpaQueryFactory.select(qWhatLine).from(qWhatLine).where(qWhatLine.createBy.eq(username)).orderBy(qWhatLine.createTime.asc()).fetch();
        WhatLine whatLine = new WhatLine();
        whatLine.setName("所有");
        list.add(0,whatLine);
        return list;
    }

    //角色配置  选择机械
    public List<ComboBoxModel> getMaschine(String name){
        List<ComboBoxModel> list = this.maschineRepository.queryMaschineByAll(name);
        return list;
    }


    public WhatLineModel<String> view(String id) throws DataNotFoundException {
        WhatLine whatLine = whatLineRepository.findById(id).orElseThrow(() -> new DataNotFoundException(ResultCode.DATA_NOT_FOUND));
        List<String> hierarchyList = this.whatLineHierarchyRepository.queryHierarchyByWhatLineId(id);
        WhatLineModel<String> whatLineModel = new WhatLineModel<>();
        whatLineModel.setWhatLine(whatLine);
        whatLineModel.setList(hierarchyList);
        return whatLineModel;
    }

    public WhatLine add(WhatLine whatLineModel) throws CustomException {
        String id = whatLineModel.getId();
        String name = whatLineModel.getName();
        String[] ids = whatLineModel.getIds();
        WhatLine whatLine = null;
        if(StringUtils.isBlank(id)){
            whatLine = new WhatLine();
        }else{
            whatLine = this.whatLineRepository.findById(id).orElse(null);
            if(whatLine == null){
                throw new CustomException("未查到产线信息！");
            }
        }
        whatLine.setName(name);
        WhatLine byName = this.whatLineRepository.getByName(name);
        if(byName != null && byName.getId() != id){
            throw new CustomException("生产线名称已存在！");
        }

        whatLine = this.whatLineRepository.save(whatLine);
        this.whatLineHierarchyRepository.deleteByWhatLineId(whatLine.getId());
        for (String hierarchyId:ids){
            this.whatLineHierarchyRepository.save(new WhatLineHierarchy(null,whatLine.getId(),hierarchyId));
        }
        return whatLine;
    }

    public void deleteWhatLine(String id){
        this.hierarchyRepository.deleteByAbilityLineId(id);
        this.whatLineRepository.deleteById(id);
    }


    public List<Hierarchy> queryAllHierarchy(){
        List<Hierarchy> hierarchyList = this.hierarchyRepository.findAll();
        List<Hierarchy> result = new ArrayList<>();
        for (Hierarchy h:hierarchyList){
            if(h.getPid() == null){
                h.setChildren(this.hierarchyService.queryTree(h,hierarchyList));
                result.add(h);
            }
        }
        return result;
    }

    public List<Hierarchy> queryHierarchyByWhatLineId(String whatLineId){
        QHierarchy qHierarchy = QHierarchy.hierarchy;
        QWhatLineHierarchy qWhatLineHierarchy = QWhatLineHierarchy.whatLineHierarchy;
        List<Hierarchy> hierarchyList = this.jpaQueryFactory.select(Projections.bean(Hierarchy.class,qHierarchy.id,qHierarchy.name,qHierarchy.number,qHierarchy.pid))
                .from(qHierarchy).leftJoin(qWhatLineHierarchy).on(qHierarchy.id.eq(qWhatLineHierarchy.hierarchyId)).where(qWhatLineHierarchy.whatLineId.eq(whatLineId))
                .fetch();
        List<Hierarchy> result = new ArrayList<>();
        for (Hierarchy h:hierarchyList){
            if(h.getPid() == null){
                h.setChildren(this.hierarchyService.queryTree(h,hierarchyList));
                result.add(h);
            }
        }
        return result;
    }

}
